package com.lanou.userLogin.order.dao;

import com.lanou.userLogin.order.domain.Order;
import com.lanou.userLogin.order.domain.OrderBook;
import com.lanou.userLogin.order.domain.OrderItem;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrderDaoImpl implements OrderDao {

    private ComboPooledDataSource dataSource = new ComboPooledDataSource();

    @Override
    public void insertOrder(String oid, String orderTime, double total, String state, String uid, String address) {
        System.out.println("44444444444");
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            QueryRunner qr = new QueryRunner();
            String sql = "insert into orders values(?,?,?,?,?,?)";
            qr.update(conn, sql, oid, orderTime, total, state, uid, address);
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void insertOrderItem(String iid, int count, String subtotal, String oid, String bid) {
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            QueryRunner runner = new QueryRunner();
            String sql = "insert into orderitem values (?,?,?,?,?)";
            runner.update(conn, sql, iid, count, subtotal, oid, bid);
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Order selectOrderByoid(String oid) {
        try {
            Connection conn = dataSource.getConnection();
            QueryRunner runner = new QueryRunner();
            String sql = "select * from orders where oid = ?";
            Order order = runner.query(conn, sql, new BeanHandler<Order>(Order.class), oid);
            conn.close();
            return order;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new Order();
    }

    @Override
    public List<OrderBook> selectOrderBook(String oid) {
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            QueryRunner runner = new QueryRunner();
            String sql = "select b.image,b.bname,b.price,b.author,o.count,b.price*o.count as totalcount from book b,orderitem o where oid = ? and o.bid = b.bid";
            List<OrderBook> orderBook = runner.query(conn, sql, new BeanListHandler<>(OrderBook.class), oid);
            conn.close();
            return orderBook;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    @Override
    public void orderPay(String oid, String address) {
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            QueryRunner runner = new QueryRunner();
            String sql = "update orders set state = 2 ,address = ? where oid = ?";
            runner.update(conn, sql, address, oid);
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Order> selectOrder(String uid) {
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            QueryRunner runner = new QueryRunner();
            String sql = "select * from orders where uid = ?";
            List<Order> orderList = runner.query(conn, sql, new BeanListHandler<>(Order.class), uid);
            conn.close();
            return orderList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    @Override
    public List<OrderItem> selectOrderItem(String oid) {
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            QueryRunner runner = new QueryRunner();
            String sql = "select * from orderitem where oid = ?";
            List<OrderItem> orderItemList = runner.query(conn, sql, new BeanListHandler<>(OrderItem.class), oid);
            conn.close();
            return orderItemList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }
}
